package org.hospital.service.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hospital.entity.BillDAO;
import org.hospital.entity.BillProject;
import org.hospital.entity.Clinic;
import org.hospital.entity.ClinicDAO;
import org.hospital.entity.ClinicFundReport;
import org.hospital.entity.ClinicFundReportDAO;
import org.hospital.entity.ClinicTreatReport;
import org.hospital.entity.ClinicTreatReportDAO;
import org.hospital.entity.Department;
import org.hospital.entity.DepartmentTreatReportDAO;
import org.hospital.entity.Project;
import org.hospital.entity.ProjectDAO;
import org.hospital.entity.TreatInfo;
import org.hospital.entity.TreatInfoDAO;
import org.hospital.service.ReportService;
import org.hospital.util.DateUtil;
import org.hospital.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class ReportServiceImpl implements ReportService {

	@Autowired
	private ProjectDAO pjDao;
	@Autowired
	private BillDAO bDao;
	@Autowired
	private ClinicDAO cDao;
	@Autowired
	private TreatInfoDAO tiDao;
	@Autowired
	private ClinicTreatReportDAO ctrDao;
	@Autowired
	private ClinicFundReportDAO cfrDao;
	@Autowired
	private DepartmentTreatReportDAO dtrDao;

	@Override
	public JSONArray getClinicTreatReport(int clinicId, int type, long datetime) throws Exception {
		Clinic clinic = cDao.findById(clinicId);
		if (clinic == null) {
			throw new Exception("门诊为空");
		}
		if (type == 0) { // 月报
			JSONArray resultJO = getClinicTreatReport_month(clinic, datetime);
			return resultJO;
		}
		// 季报
		JSONArray resultJO = getClinicTreatReport_season(clinic, datetime);
		return resultJO;
	}
	
	private JSONArray getClinicTreatReport_month(Clinic clinic, long datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 查询月报开始时间 查询月份第一天
		long startTime = DateUtil.getMonthFirstDayLong(datetime);
		// 查询月报截至时间 查询月份下一个月第一天
		long endTime = DateUtil
				.getShortDateLong(DateUtil.addMonth(sdf.format(new Date(startTime))));

		if (isNowMonth(datetime)) { // 系统当前月 查看bill、treatinfo表获取报表数据
			System.out.println("getClinicTreatReport_month 系统当前月 查看bill、treatinfo表获取报表数据");
			List<Project> pjList = pjDao.getClinicTreatmentProject(clinic.getClinicId(), startTime,
					endTime);
			JSONArray resultJA = getClinicTreatReportMsg(pjList, clinic, startTime, endTime);
			return resultJA;
		}

		// 非系统当前月份 查看report表获取报表数据
		Map paramMap = new HashMap<>();
		paramMap.put("type", 0);
		paramMap.put("clinicId", clinic.getClinicId());
		paramMap.put("datetime", new Date(startTime));

		List<ClinicTreatReport> ctrList = ctrDao.getByProperties(paramMap);
		if (null == ctrList || ctrList.size() == 0) { // 如果没有记录
			System.out.println("getClinicTreatReport_month 如果没有记录");
			List<Project> pjList = pjDao.getClinicTreatmentProject(clinic.getClinicId(), startTime,
					endTime);
			JSONArray resultJA = getClinicTreatReportMsg(pjList, clinic, startTime, endTime);

			for (int i = 0; i < resultJA.size(); i++) { // 添加报表数据至report表
				JSONObject ctrJO = resultJA.getJSONObject(i);
				ClinicTreatReport ctr = new ClinicTreatReport();
				ctr.setClinicId(clinic.getClinicId());
				ctr.setProjectId(ctrJO.getInteger("projectId"));
				ctr.setDatetime(new Timestamp(startTime));
				ctr.setType(0);
				ctr.setTreatIncome(ctrJO.getDouble("cureCost"));// 治疗收入
				ctr.setOtherIncome(ctrJO.getDouble("branchIncome"));// 抽成收入
				ctr.setSelfCost(ctrJO.getDouble("centralCost"));// 本院支出
				ctr.setOtherCost(ctrJO.getDouble("branchCost")); // 分院支出

				ctrDao.merge(ctr);
			}
			return resultJA;
		}

		// 如果有数据
		System.out.println("getClinicTreatReport_month 如果有记录");
		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < ctrList.size(); i++) {
			ClinicTreatReport ctr = ctrList.get(i);
			Project pj = pjDao.findById(ctr.getProjectId());
			JSONObject resultJO = new JSONObject();
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("hospitalName", clinic.getName());
			resultJO.put("projectId", pj.getProjectId());
			resultJO.put("cureName", pj.getName());
			resultJO.put("cureCost", ctr.getTreatIncome());
			resultJO.put("centralCost", ctr.getSelfCost());
			resultJO.put("branchCost", ctr.getOtherCost());
			resultJO.put("branchIncome", ctr.getOtherIncome());
			double profit = ctr.getTreatIncome() + ctr.getOtherIncome() - ctr.getOtherCost()
					- ctr.getSelfCost();
			resultJO.put("centeralIncome", DoubleUtil.parseDoubleTwo(profit));
			resultJA.add(resultJO);
		}

		return resultJA;
	}
	
	private JSONArray getClinicTreatReport_season(Clinic clinic, long datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		Date date = new Date(datetime);
		cal.setTime(date);
		
		// 查询季报开始时间 查询季度的第一天
		long startTime = DateUtil.getSeasonFirstDay(cal.get(Calendar.YEAR), DateUtil.getSeason(date));
		// 查询季报截至时间 查询下一个季度的第一天
		String endTimeStr = DateUtil.addThreeMonth(sdf.format(new Date(startTime)));
		long endTime = DateUtil.getShortDateLong(endTimeStr);
		//System.out.println("getClinicTreatReport_season.startTime" + new Date(startTime));
		//System.out.println("getClinicTreatReport_season.endTime" + new Date(endTime));
		
		if (isNowSeason(datetime)) { //如果是当前季度 查看bill、treatInfo表获取数据
			System.out.println("getClinicTreatReport_season 如果是当前季度 查看bill、treatInfo表获取数据");
			List<Project> pjList = pjDao.getClinicTreatmentProject(clinic.getClinicId(), startTime,
					endTime);
			JSONArray resultJA = getClinicTreatReportMsg(pjList, clinic, startTime, endTime);
			return resultJA;
		}
		
		//非当前季度 查看report报表数据
		Map paramMap = new HashMap<>();
		paramMap.put("type", 1);
		paramMap.put("clinicId", clinic.getClinicId());
		paramMap.put("datetime", new Date(startTime));
		List<ClinicTreatReport> ctrList = ctrDao.getByProperties(paramMap);
		if(ctrList == null || ctrList.size() == 0) {
			//如果报表没有数据
			System.out.println("getClinicTreatReport_season 如果报表没有数据");
			List<Project> pjList = pjDao.getClinicTreatmentProject(clinic.getClinicId(), startTime,
					endTime);
			JSONArray resultJA = getClinicTreatReportMsg(pjList, clinic, startTime, endTime);
			
			for (int i = 0; i < resultJA.size(); i++) { // 添加报表数据至report表
				JSONObject ctrJO = resultJA.getJSONObject(i);
				ClinicTreatReport ctr = new ClinicTreatReport();
				ctr.setClinicId(clinic.getClinicId());
				ctr.setProjectId(ctrJO.getInteger("projectId"));
				ctr.setDatetime(new Timestamp(startTime));
				ctr.setType(1);
				ctr.setTreatIncome(ctrJO.getDouble("cureCost"));// 治疗收入
				ctr.setOtherIncome(ctrJO.getDouble("branchIncome"));// 抽成收入
				ctr.setSelfCost(ctrJO.getDouble("centralCost"));// 本院支出
				ctr.setOtherCost(ctrJO.getDouble("branchCost")); // 分院支出

				ctrDao.merge(ctr);
			}
			return resultJA;
		}
		//如果report表有数据
		System.out.println("getClinicTreatReport_season 如果report表有数据");
		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < ctrList.size(); i++) {
			ClinicTreatReport ctr = ctrList.get(i);
			Project pj = pjDao.findById(ctr.getProjectId());
			JSONObject resultJO = new JSONObject();
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("hospitalName", clinic.getName());
			resultJO.put("projectId", pj.getProjectId());
			resultJO.put("cureName", pj.getName());
			resultJO.put("cureCost", ctr.getTreatIncome());
			resultJO.put("centralCost", ctr.getSelfCost());
			resultJO.put("branchCost", ctr.getOtherCost());
			resultJO.put("branchIncome", ctr.getOtherIncome());
			double profit = ctr.getTreatIncome() + ctr.getOtherIncome() - ctr.getOtherCost()
					- ctr.getSelfCost();
			resultJO.put("centeralIncome", DoubleUtil.parseDoubleTwo(profit));
			resultJA.add(resultJO);
		}

		return resultJA;
	}


	private JSONArray getClinicTreatReportMsg(List<Project> pjList, Clinic clinic, long startTime,
			long endTime) {
		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < pjList.size(); i++) {
			Project project = (Project) pjList.get(i);
			JSONObject resultJO = new JSONObject();

			double sumIncome = 0.0; // 治疗项目总收入
			double sumOtherClinicIncome = 0.0; // 获取分院抽成成本
			double sumDoctorCost = 0.0; // 医生抽成总成本
			double sumOperatorCost = 0.0; // 本部执行者抽成成本
			double sumOtherOperatorCost = 0.0; // 分部执行者抽成成本
			double sumOtherClinicCost = 0.0; // 分部门诊抽成成本

			// 获取治疗项目开单详细信息
			List<BillProject> bpjList = bDao.getBillProjectSummary(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime, 1);
			for (int j = 0; j < bpjList.size(); j++) {
				BillProject billProject = (BillProject) bpjList.get(j);
				double income = billProject.getPrice();
				double doctorCost = (income * billProject.getDoctorCost()) / 100.0;
				sumIncome += income;
				sumDoctorCost += doctorCost;
			}

			// 获取本院开单的所有客户治疗信息的详细信息
			List<TreatInfo> tiList = tiDao.getClinicTreatInfo(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime);
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo treatInfo = tiList.get(j);
				if (treatInfo.getClinicId() == clinic.getClinicId()) {
					sumOperatorCost += (treatInfo.getUnitPrice() * treatInfo.getExcutorCost()) / 100.0;
					continue;
				}
				sumOtherOperatorCost += (treatInfo.getUnitPrice() * treatInfo.getExcutorCost()) / 100.0;
				sumOtherClinicCost += (treatInfo.getUnitPrice() * treatInfo.getClinicCost()) / 100.0;
			}

			// 获取本院 执行者执行其他门诊单据的治疗信息
			List<TreatInfo> tiList2 = tiDao.getOtherTreatInfo(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime, 1);
			for (int j = 0; j < tiList2.size(); j++) {
				TreatInfo ti = tiList2.get(j);
				sumOperatorCost += (ti.getUnitPrice() * ti.getExcutorCost() / 100);
				sumOtherClinicIncome += (ti.getUnitPrice() * ti.getClinicCost() / 100);
			}

			// 插入数据
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("hospitalName", clinic.getName());
			resultJO.put("projectId", project.getProjectId());
			resultJO.put("cureName", project.getName());
			resultJO.put("cureCost", DoubleUtil.parseDoubleTwo(sumIncome));
			resultJO.put("centralCost", DoubleUtil.parseDoubleTwo(sumDoctorCost + sumOperatorCost));
			resultJO.put("branchCost",
					DoubleUtil.parseDoubleTwo(sumOtherClinicCost + sumOtherOperatorCost));
			double sumProfit = sumIncome - sumDoctorCost - sumOperatorCost - sumOtherClinicCost
					- sumOtherOperatorCost + sumOtherClinicIncome;
			resultJO.put("centeralIncome", DoubleUtil.parseDoubleTwo(sumProfit));
			resultJO.put("branchIncome", DoubleUtil.parseDoubleTwo(sumOtherClinicIncome));

			resultJA.add(resultJO);
		}

		return resultJA;
	}

	@Override
	public JSONArray getClinicFundReport(int clinicId, int type, long datetime) throws Exception {
		Clinic clinic = cDao.findById(clinicId);
		if (clinic == null) {
			throw new Exception("门诊为空");
		}
		if (type == 0) { // 月报
			JSONArray resultJO = getClinicFundReport_month(clinic, datetime);
			return resultJO;
		}
		// 季报
		JSONArray resultJO = getClinicFundReport_season(clinic, datetime);
		return resultJO;
	}
	
	/**
	 * 获取门诊费用项目月报表
	 * @param clinic
	 * @param datetime
	 * @return	[projectId,projectName,type,clinicId,clinicName,price]
	 */
	private JSONArray getClinicFundReport_month(Clinic clinic,long datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 查询月报开始时间 查询月份第一天
		long startTime = DateUtil.getMonthFirstDayLong(datetime);
		// 查询月报截至时间 查询月份下一个月第一天
		long endTime = DateUtil
				.getShortDateLong(DateUtil.addMonth(sdf.format(new Date(startTime))));

		if (isNowMonth(datetime)) { // 系统当前月 查看bill、treatinfo表获取报表数据
			System.out.println("getClinicFundReport_month 系统当前月 查看bill、treatinfo表获取报表数据");
			JSONArray resultJA = getClinicFundReportMsg(clinic, startTime, endTime);
			return resultJA;
		}
		// 非系统当前月份 查看report表获取报表数据
		Map paramMap = new HashMap<>();
		paramMap.put("type", 0);
		paramMap.put("clinicId", clinic.getClinicId());
		paramMap.put("datetime", new Date(startTime));
		List<ClinicFundReport> cfrList = cfrDao.getByProperties(paramMap);
		if(cfrList == null || cfrList.size() == 0) {	//如果report表没有数据
			System.out.println("getClinicFundReport_month 如果report表没有数据");
			JSONArray resultJA = getClinicFundReportMsg(clinic, startTime, endTime);
			
			for (int i = 0; i < resultJA.size(); i++) {
				JSONObject resultJO = resultJA.getJSONObject(i);
				
				ClinicFundReport cfr = new ClinicFundReport();
				cfr.setClinicId(clinic.getClinicId());
				cfr.setDatetime(new Timestamp(startTime));
				cfr.setPrice(resultJO.getDouble("price"));
				cfr.setProjectId(resultJO.getInteger("projectId"));
				cfr.setType(0); //0:月报；1季报
				cfrDao.merge(cfr);
			}
			
			return resultJA;
		}
		//如果report表有数据
		System.out.println("getClinicFundReport_month 如果report表有数据");
		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < cfrList.size(); i++) {
			ClinicFundReport cfr = cfrList.get(i);
			
			JSONObject resultJO = new JSONObject();
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("projectId", cfr.getProjectId());
			if(cfr.getProjectId().equals(0)) {	//如果projectId==0;默认是治疗项目汇总信息
				resultJO.put("projectName", "治疗项目");
				resultJO.put("type", "收入");
			}else {
				Project pj = pjDao.findById(cfr.getProjectId());
				if (pj.getType() == 0) {
					resultJO.put("type", "收入");
				}
				if (pj.getType() == 1) {
					resultJO.put("type", "支出");
				}
				resultJO.put("projectName", pj.getName());
			}
			resultJO.put("price", cfr.getPrice());
			resultJA.add(resultJO);
		}
		
		return resultJA;
	}
	/**
	 * 获取门诊费用项目季度报表
	 * @param clinic
	 * @param datetime
	 * @return	[projectId,projectName,type,clinicId,clinicName,price]
	 */
	private JSONArray getClinicFundReport_season(Clinic clinic,long datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		Date date = new Date(datetime);
		cal.setTime(date);
		
		// 查询季报开始时间 查询季度的第一天
		long startTime = DateUtil.getSeasonFirstDay(cal.get(Calendar.YEAR), DateUtil.getSeason(date));
		// 查询季报截至时间 查询下一个季度的第一天
		String endTimeStr = DateUtil.addThreeMonth(sdf.format(new Date(startTime)));
		long endTime = DateUtil.getShortDateLong(endTimeStr);
		
		if (isNowSeason(datetime)) { // 系统当前季度 查看bill、treatinfo表获取报表数据
			System.out.println("getClinicFundReport_season 系统当前季度 查看bill、treatinfo表获取报表数据");
			JSONArray resultJA = getClinicFundReportMsg(clinic, startTime, endTime);
			return resultJA;
		}
		// 非系统当前季度 查看report表获取报表数据
		Map paramMap = new HashMap<>();
		paramMap.put("type", 1);
		paramMap.put("clinicId", clinic.getClinicId());
		paramMap.put("datetime", new Date(startTime));
		List<ClinicFundReport> cfrList = cfrDao.getByProperties(paramMap);
		if(cfrList == null || cfrList.size() == 0) {	//如果report表没有数据
			System.out.println("getClinicFundReport_season 如果report表没有数据");
			JSONArray resultJA = getClinicFundReportMsg(clinic, startTime, endTime);
			
			for (int i = 0; i < resultJA.size(); i++) {
				JSONObject resultJO = resultJA.getJSONObject(i);
				//插入clinicFundReport表
				ClinicFundReport cfr = new ClinicFundReport();
				cfr.setClinicId(clinic.getClinicId());
				cfr.setDatetime(new Timestamp(startTime));
				cfr.setPrice(resultJO.getDouble("price"));
				cfr.setProjectId(resultJO.getInteger("projectId"));
				cfr.setType(1); //0:月报；1季报
				cfrDao.merge(cfr);
			}
			
			return resultJA;
		}
		//如果report表有数据
		System.out.println("getClinicFundReport_season 如果report表有数据");
		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < cfrList.size(); i++) {
			ClinicFundReport cfr = cfrList.get(i);
			
			JSONObject resultJO = new JSONObject();
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("projectId", cfr.getProjectId());
			if(cfr.getProjectId().equals(0)) {	//如果projectId==0;默认是治疗项目汇总信息
				resultJO.put("projectName", "治疗项目");
				resultJO.put("type", "收入");
			}else {
				Project pj = pjDao.findById(cfr.getProjectId());
				if (pj.getType() == 0) {
					resultJO.put("type", "收入");
				}
				if (pj.getType() == 1) {
					resultJO.put("type", "支出");
				}
				resultJO.put("projectName", pj.getName());
			}
			resultJO.put("price", cfr.getPrice());
			resultJA.add(resultJO);
		}
		
		return resultJA;
	}
	
	private JSONArray getClinicFundReportMsg(Clinic clinic,long startTime,long endTime) {
		// [clinicId,clinicName,projectId,projectName,type,price]
				JSONArray resultJA = new JSONArray();
				// 获取[startTime,endTime]所有有效的(收入、支出)费用项目
				List<Project> pjFundList = pjDao.getClinicFundProject(clinic.getClinicId(), startTime,
						endTime);
				// System.out.println("getClinicFundProjectReport.pjFundList : "+JSON.toJSONString(pjFundList,true));
				for (int i = 0; i < pjFundList.size(); i++) {
					Project pj = pjFundList.get(i);
					JSONObject resultJO = new JSONObject();
					// 获取费用项目具体开单细节
					List<BillProject> bpList = bDao.getFundBPSummary(clinic.getClinicId(),
							pj.getProjectId(), startTime, endTime);
					// System.out.println("getClinicFundProjectReport.bpList : "+JSON.toJSONString(bpList,true));
					// 计算指定费用项目总价格
					double price = 0.0;
					for (int j = 0; j < bpList.size(); j++) {
						BillProject bp = bpList.get(j);
						price += bp.getPrice();
					}
					// 格式化数据
					resultJO.put("clinicId", clinic.getClinicId());
					resultJO.put("clinicName", clinic.getName());
					resultJO.put("projectId", pj.getProjectId());
					resultJO.put("projectName", pj.getName());
					resultJO.put("price", DoubleUtil.parseDoubleTwo(price));
					if (pj.getType() == 0) {
						resultJO.put("type", "收入");
					}
					if (pj.getType() == 1) {
						resultJO.put("type", "支出");
					}
					resultJA.add(resultJO);
				}
				// 获取[startTime,endTime]所有有效的治疗项目收入
				List<Project> pjTreatList = pjDao.getClinicTreatmentProject(clinic.getClinicId(),
						startTime, endTime);

				// 治疗项目获取的总利润=开单收入+门诊抽成收入-门诊抽成支出-执行者支出-医生支出
				double treatProfit = 0.0;
				for (int i = 0; i < pjTreatList.size(); i++) {
					Project project = (Project) pjTreatList.get(i);

					double sumIncome = 0.0; // 开单收入
					double sumOtherClinicIncome = 0.0; // 门诊抽成收入
					double sumDoctorCost = 0.0; // 医生支出
					double sumOperatorCost = 0.0; // 执行者支出
					double sumOtherClinicCost = 0.0; // 门诊抽成支出

					// 获取治疗项目开单详细信息
					List<BillProject> bpjList = bDao.getBillProjectSummary(clinic.getClinicId(),
							project.getProjectId(), startTime, endTime, 1);
					for (int j = 0; j < bpjList.size(); j++) {
						BillProject billProject = (BillProject) bpjList.get(j);
						double income = billProject.getPrice();
						double doctorCost = (income * billProject.getDoctorCost()) / 100.0;
						sumIncome += income;
						sumDoctorCost += doctorCost;
					}

					// 获取本院开单的所有客户治疗信息的详细信息
					List<TreatInfo> tiList = tiDao.getClinicTreatInfo(clinic.getClinicId(),
							project.getProjectId(), startTime, endTime);
					for (int j = 0; j < tiList.size(); j++) {
						TreatInfo treatInfo = tiList.get(j);
						//如果是本院
						if (treatInfo.getClinicId() == clinic.getClinicId()) {
							sumOperatorCost += (treatInfo.getUnitPrice() * treatInfo.getExcutorCost()) / 100.0;
							continue;
						}
						//如果是分院
						sumOperatorCost += (treatInfo.getUnitPrice() * treatInfo.getExcutorCost()) / 100.0;
						sumOtherClinicCost += (treatInfo.getUnitPrice() * treatInfo.getClinicCost()) / 100.0;
					}

					// 获取本院 执行者执行其他门诊单据的治疗信息
					List<TreatInfo> tiList2 = tiDao.getOtherTreatInfo(clinic.getClinicId(),
							project.getProjectId(), startTime, endTime, 1);
					for (int j = 0; j < tiList2.size(); j++) {
						TreatInfo ti = tiList2.get(j);
						sumOperatorCost += (ti.getUnitPrice() * ti.getExcutorCost() / 100);
						sumOtherClinicIncome += (ti.getUnitPrice() * ti.getClinicCost() / 100);
					}
					treatProfit += (sumIncome+sumOtherClinicIncome-sumDoctorCost-sumOperatorCost-sumOtherClinicCost);
				}
				JSONObject resultJO = new JSONObject();
				resultJO.put("clinicId", clinic.getClinicId());
				resultJO.put("clinicName", clinic.getName());
				resultJO.put("projectId", 0);
				resultJO.put("projectName", "治疗项目");
				resultJO.put("type", "收入");
				resultJO.put("price", DoubleUtil.parseDoubleTwo(treatProfit));
				resultJA.add(resultJO);
				
				return resultJA;
	}
	
	@Override
	public JSONArray getDepartmentTreatReport(int departmentId, int type, long datetime) throws Exception {
		Clinic clinic = cDao.findById(departmentId);
		if (clinic == null) {
			throw new Exception("门诊为空");
		}
		if (type == 0) { // 月报
			JSONArray resultJO = getClinicFundReport_month(clinic, datetime);
			return resultJO;
		}
		// 季报
		JSONArray resultJO = getClinicFundReport_season(clinic, datetime);
		return resultJO;
	}
	
	private JSONArray getDpmTreatReport_month(Department dpm,long datetime) {
		return null;
	}
	
	private JSONArray getDpmTreatReport_season(Department dpm,long datetime) {
		return null;
	}
	
	
	/**
	 * 判断是否是当月
	 * 
	 * @param datetime
	 * @return true:是本月份;false:不是本月份
	 */
	private boolean isNowMonth(long datetime) {
		Calendar dtCalendar = Calendar.getInstance();
		dtCalendar.setTime(new Date(datetime));
		
		Calendar nowCalendar = Calendar.getInstance();
		nowCalendar.setTime(new Date());
		//System.out.println("isNowMonth.dtCalendar" + dtCalendar.getTime());
		//System.out.println("isNowMonth.nowCalendar" + nowCalendar.getTime());
		
		if(dtCalendar.get(Calendar.YEAR) == nowCalendar.get(Calendar.YEAR)) {
			int dtMonth = dtCalendar.get(Calendar.MONTH);
			int nowMonth = nowCalendar.get(Calendar.MONTH);
			if(dtMonth == nowMonth) {
				return true;
			}
			return false;
		}
		
		return false;
	}
	
	/**
	 * 判断是否是同年同季节
	 * @param datetime
	 * @return	true:是同年同季节;false:不同
	 */
	private boolean isNowSeason(long datetime) {
		if(isNowYear(datetime)) {
			int datetime_season = DateUtil.getSeason(new Date(datetime));
			int now_season = DateUtil.getSeason(new Date());
			if(datetime_season == now_season) {
				return true;
			}
			return false;
			
		}else {
			return false;
		}
	}
	
	/**
	 * 判断是否是同年
	 * @param datetime
	 * @return	true:是同年;false:不同
	 */
	private boolean isNowYear(long datetime) {
		Calendar dtCalendar = Calendar.getInstance();
		dtCalendar.setTime(new Date(datetime));
		
		Calendar nowCalendar = Calendar.getInstance();
		nowCalendar.setTime(new Date());
		//System.out.println("isNowYear.dtCalendar" + dtCalendar.getTime());
		//System.out.println("isNowYear.nowCalendar" + nowCalendar.getTime());
		
		if(dtCalendar.get(Calendar.YEAR) == nowCalendar.get(Calendar.YEAR)) {
			return true;
		}
		return false;
		
	}
}
